package com.fafu;

import com.fafu.model.Student;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Select {
    public static void main(String[] args) throws Exception{
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要查询的学生姓名,可模糊搜索");
        String name = scanner.next();

        List<Student> list = new ArrayList<>();
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/student?serverTimezone=GMT%2B8&useSSL=false&characterEncoding=UTF-8","root","root");
        String sql = "SELECT * FROM `student` WHERE `studentname` LIKE CONCAT('%',?, '%')";
        PreparedStatement pstm = connection.prepareStatement(sql);
        pstm.setString(1,name);

        ResultSet resultSet = pstm.executeQuery();
        while (resultSet.next()){
            Student student = new Student();
            student.setStudentNo(resultSet.getString("studentno"));
            student.setStudentName(resultSet.getString("studentname"));
            student.setSex(resultSet.getString("studentname").charAt(0));
            list.add(student);
        }
        for(Student student:list){
            System.out.println(student.getSex());
            System.out.println(student.getStudentName());
            System.out.println(student.getStudentNo());
        }
        pstm.close();
        connection.close();
    }

}
